/**
 * tjftp -- A simple FTP server, using the ANSI C
 * Copyright (C) 2011  degui.chen <degui.chen@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "command.h"
#include "lib/string.h"
#include "lib/unity.h"

//如果你想添加其他命令，请在这里包含下它的头文件，配置好命令的映射字符串和关联对应的回调函数。
#include "cmd/cmd.h"
static Command cmds[] = {
		{ "ABOR", tj_cmd_abor },
		{ "CWD", tj_cmd_cwd },
		{ "DELE", tj_cmd_dele },
		{ "LIST", tj_cmd_list },
		{ "MDTM", tj_cmd_mdtm },
		{ "MKD", tj_cmd_mkd },
		{ "NLST", tj_cmd_nlst },
		{ "PASS", tj_cmd_pass },
		{ "PASV", tj_cmd_pasv },
		{ "PORT", tj_cmd_port },
		{ "PWD", tj_cmd_pwd },
		{ "QUIT", tj_cmd_quit },
		{ "RETR", tj_cmd_retr },
		{ "RMD", tj_cmd_rmd },
		{ "RNFR", tj_cmd_rnfr },
		{ "RNTO", tj_cmd_rnto },
		{ "SITE", tj_cmd_site },
		{ "SIZE", tj_cmd_size },
		{ "STOR", tj_cmd_stor },
		{ "TYPE", tj_cmd_type },
		{ "USER", tj_cmd_user },
		{ "ACCT", tj_cmd_acct },
		{ "APPE", tj_cmd_appe },
		{ "CDUP", tj_cmd_cdup },
		{ "HELP", tj_cmd_help },
		{ "MODE", tj_cmd_mode },
		{ "NOOP", tj_cmd_noop },
		{ "REIN", tj_cmd_rein },
		{ "STAT", tj_cmd_stat },
		{ "STOU", tj_cmd_stou },
		{ "STRU", tj_cmd_stru },
		{ "SYST", tj_cmd_syst }
};

/*
 * 返回长度
 */
int tj_command_exec(int accept_fd, char* request, char* response) {
	DEBUG_LOG("对回复内容使用线程", 0); //先做命令模式？ 在命令模式中使用线程作为响应？
	tj_string_trim_left(request);
	int i;
	for (i = 0; i < sizeof(cmds) / sizeof(Command); i++) {
		if (!strncmp(cmds[i].cmd, request, strlen(cmds[i].cmd))) {
			CommandHandler handler = cmds[i].handler;
			return handler(accept_fd, request, response);//返回响应结果的长度。
		}
	}
	return -1;
}

